package com.atsmart.app.activity;

import java.io.IOException;
import java.io.Serializable;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.activity.ProductListActivity.Item;
import com.atsmart.app.exception.BizException;
import com.atsmart.app.model.ProductItemInfo;
import com.atsmart.app.model.ProductItemListInfo;
import com.atsmart.app.sql.ProductItemSqlHelper;
import com.atsmart.app.utils.PersistentKeyUtil;
import com.atsmart.app.utils.PrefsUtils;
import com.atsmart.app.utils.StringUtil;
import com.atsmart.app.webservice.ServiceAsynTask;
import com.atsmart.app.webservice.ServiceHelper;
import com.atsmart.app.webservice.ServiceHost;
import com.google.json.Gson;
import com.google.json.JsonParseException;
import com.google.json.annotations.SerializedName;


public class ProductItemActivity extends BaseActivity {
	public static final String TAG = ProductItemActivity.class.getName();
	private int templateID;
	private ProductItemListInfo detailInfo;
	private LinearLayout rootLayout;
	private ProductItemSqlHelper dsql = null;
	private TextView title;
	private ProductItemSqlHelper isql = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_item_layout);
		rootLayout = (LinearLayout) findViewById(R.id.root_layout);
		dsql = new ProductItemSqlHelper();
		dsql.open(BizApp.getInstance().getSqlHelper());
		Bundle bundle = getIntent().getExtras();
		templateID = bundle.getInt(PersistentKeyUtil.CLIENT_ID_STRING, 0);
		detailInfo = dsql.getProductItemInfo(templateID);
		isql = new ProductItemSqlHelper();
		isql.open(BizApp.getInstance().getSqlHelper());
		title = (TextView) findViewById(R.id.title);
		title.setText(bundle.getString(PersistentKeyUtil.PRODUCT_NAME));
//		if (detailInfo != null && detailInfo.getProducts() != null
//				&& detailInfo.getProducts().size() > 0) {
//			generateTemplate();
//			
//		}
		getUpdateItemData(templateID);
	}

	public class Item implements Serializable {

		/**
		 *
		 */
		private static final long serialVersionUID = -6760611499805749529L;
		@SerializedName("product-id")
		public int product_id;
	}

	private void getUpdateItemData(final int item) {
		showLoading();
		registerLoadStatus(TAG);
		new ServiceAsynTask<ProductItemListInfo>() {

			@Override
			protected ProductItemListInfo callService() throws IOException,
					JsonParseException, AssertionError, BizException, Exception {
				Item pageInfo = new Item();
				pageInfo.product_id = item;
				return ServiceHelper.getInstance().postData(
						ServiceHost.productitem(), new Gson().toJson(pageInfo),
						ProductItemListInfo.class);
			}

			@Override
			protected void runWithResult(ProductItemListInfo result)
					throws Exception {
				if (result != null) {
					isql.deleteAttribute(item);
					for (ProductItemInfo info : result.getProducts()) {
						isql.addProductItemInfo(info, item);
					}
					generateTemplate(result);
				}
				// TaskForAllActivity.hideLoading();
				hideLoading();
				finishedLoad(TAG);
			}

			@Override
			protected void runWithError(ProductItemListInfo result,
					int iErrorCode) throws Exception {
				loadingFailed(TAG, iErrorCode);
				hideLoading();
				if(PrefsUtils.getURL().equals(StringUtil.URL1)){
					PrefsUtils.setURL(StringUtil.URL2);
				}else if(PrefsUtils.getURL().equals(StringUtil.URL2)){
					PrefsUtils.setURL(StringUtil.URL1);
				}
				try{
					getUpdateItemDataByUrl2(item);
				}catch (Exception e) {
				}
			}
		}.execute();
	}
	
	private void getUpdateItemDataByUrl2(final int item) {
		showLoading();
		registerLoadStatus(TAG);
		new ServiceAsynTask<ProductItemListInfo>() {

			@Override
			protected ProductItemListInfo callService() throws IOException,
					JsonParseException, AssertionError, BizException, Exception {
				Item pageInfo = new Item();
				pageInfo.product_id = item;
				return ServiceHelper.getInstance().postData(
						ServiceHost.productitem(), new Gson().toJson(pageInfo),
						ProductItemListInfo.class);
			}

			@Override
			protected void runWithResult(ProductItemListInfo result)
					throws Exception {
				if (result != null) {
					isql.deleteAttribute(item);
					for (ProductItemInfo info : result.getProducts()) {
						isql.addProductItemInfo(info, item);
					}
					generateTemplate(result);
				}
				// TaskForAllActivity.hideLoading();
				hideLoading();
				finishedLoad(TAG);
			}

			@Override
			protected void runWithError(ProductItemListInfo result,
					int iErrorCode) throws Exception {
				loadingFailed(TAG, iErrorCode);
				// TaskForAllActivity.hideLoading();
				hideLoading();
				if(PrefsUtils.getURL().equals(StringUtil.URL1)){
					PrefsUtils.setURL(StringUtil.URL2);
				}else if(PrefsUtils.getURL().equals(StringUtil.URL2)){
					PrefsUtils.setURL(StringUtil.URL1);
				}
			}
		}.execute();
	}

	private void generateTemplate(ProductItemListInfo result) {
		for (ProductItemInfo itemInfo : result.getProducts()) {
			LinearLayout layout = (LinearLayout) LayoutInflater.from(this)
					.inflate(R.layout.textviewandtextview, null);
			TextView textView = (TextView) layout.getChildAt(0);
			textView.setText(itemInfo.getName());
			TextView editText = (TextView) layout.getChildAt(1);
//			editText.setId(itemInfo.getId());
//			items.put(itemInfo.getId(), ITEM_TYPE_EDITVIEW);
			editText.setText(itemInfo.getValue());
			rootLayout.addView(layout);
		}
	}

	
	private void generateTemplate() {
		for (ProductItemInfo itemInfo : detailInfo.getProducts()) {
			LinearLayout layout = (LinearLayout) LayoutInflater.from(this)
					.inflate(R.layout.textviewandtextview, null);
			TextView textView = (TextView) layout.getChildAt(0);
			textView.setText(itemInfo.getName());
			TextView editText = (TextView) layout.getChildAt(1);
//			editText.setId(itemInfo.getId());
//			items.put(itemInfo.getId(), ITEM_TYPE_EDITVIEW);
			editText.setText(itemInfo.getValue());
			rootLayout.addView(layout);
		}
	}

}
